package com.innovation.chengzhangriji.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhotoContent;
import com.innovation.chengzhangriji.util.Size;
import com.nostra13.universalimageloader.core.ImageLoader;

public class HistoryPhotoContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    // 列数
    public static final int COLUMN_NUM = 4;
    // 列的间距 (dp)
    private static final float COLUMN_SPACE = 3.5f;
    // 行的间距 (dp)
    private static final float ROW_SPACE = COLUMN_SPACE;
    // 单个图片的大小
    private Size size;
    private DisplayMetrics metrics;
    // 点击监听器
    private PhotoClickListener clickListener;

    public HistoryPhotoContentHolder(View itemView, PhotoClickListener clickListener) {
        super(itemView);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);
        itemView.setLayoutParams(params);
        size = computeCellSize();
        this.clickListener = clickListener;
    }

    // 计算列宽
    private Size computeCellSize() {
        metrics = itemView.getResources().getDisplayMetrics();
        float width = (metrics.widthPixels - metrics.density * 10 * 2 - metrics.density * COLUMN_SPACE * (COLUMN_NUM - 1)) / COLUMN_NUM;
        float height = width;
        return new Size((int)width, (int)height);
    }

    // 防止图片列表
    public void setPhotoList(UIHistoryPhotoContent photoContent) {
        ((RelativeLayout)itemView).removeAllViews();
        String[] photos = new String[photoContent.getThumbnailUrls().size()];
        photoContent.getThumbnailUrls().toArray(photos);
        for(int i = 0; i < photoContent.getThumbnailUrls().size(); i++) {
            ImageView imageView = new ImageView(itemView.getContext());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size.getWidth(), size.getHeight());
            int rowIndex = i / COLUMN_NUM;
            int colIndex = i % COLUMN_NUM;
            int left = (int) (colIndex * (size.getWidth() + metrics.density * COLUMN_SPACE));
            int top = (int) (rowIndex * (size.getHeight() + metrics.density * ROW_SPACE));
            params.setMargins(left, top, 0, 0);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setTag(i);
            imageView.setOnClickListener(this);
            if(photos[i].startsWith("/")) {
                ImageLoader.getInstance().displayImage("file://" + photos[i], imageView, Config.getOptionsPhoto());
            } else {
                ImageLoader.getInstance().displayImage(photos[i], imageView, Config.getOptionsPhoto());
            }
            ((RelativeLayout) itemView).addView(imageView);
        }
    }

    @Override
    public void onClick(View v) {
        if(clickListener != null) {
            clickListener.onPhotoClick(this, Integer.parseInt(v.getTag().toString()));
        }
    }

    /**
     * 点击事件
     */
    public interface PhotoClickListener  {
        void onPhotoClick(HistoryPhotoContentHolder holder, int index);
    }
}
